package com.usvisatrack.core.factories;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.usvisatrack.core.common.Factory;
import com.usvisatrack.core.dao.model.CheckeeVisa;
import com.usvisatrack.core.dao.model.USEmbassy;
import com.usvisatrack.core.dao.model.UscisCase;
import com.usvisatrack.core.dao.model.UscisCaseStatus;
import com.usvisatrack.core.dao.model.UscisForm;
import com.usvisatrack.core.dao.model.Visa;
import com.usvisatrack.core.dao.model.VisaClass;

/**
 * Item Data Factory
 * 
 * @author YuCheng Hu
 *
 */
public class UscisCaseFactory extends Factory {
	private static Logger logger = LoggerFactory.getLogger(UscisCaseFactory.class);

	private static final int LISTING_PAGE_SIZE = 20;
	public static final int DEFAULT_AUTH_EXPIRATION = 10;

	/**
	 * 
	 * @param uscisFormCode
	 * @return
	 */
	public static UscisCase getUscisCaseByCaseNumber(String uscisCaseNumber) {
		logger.debug("Get UscisCase OBJ by form case number - [{}]", uscisCaseNumber);

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(UscisCase.class);
			criteria.add(Restrictions.eq("uscisCaseNumber", uscisCaseNumber));
			criteria.setMaxResults(1);

			return (UscisCase) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search UscisCase data error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return null;
	}

	/**
	 * Get Visa OBJ
	 * 
	 * @param checkkeID
	 * @return
	 */
	@Deprecated
	public static UscisForm getUscisFormByCode(String uscisFormCode) {
		logger.debug("Get UscisForm OBJ by form code - [{}]", uscisFormCode);

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(UscisForm.class);
			criteria.add(Restrictions.ilike("uscisFormCode", "%" + uscisFormCode + "%"));
			criteria.setMaxResults(1);

			return (UscisForm) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search UscisForm data error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return null;
	}

	/**
	 * 
	 * @param uscisCaseStatusName
	 * @return
	 */
	public static UscisCaseStatus getUscisCaseStatusByCode(String uscisCaseStatusName) {
		logger.debug("Get UscisForm OBJ by form code - [{}]", uscisCaseStatusName);

		try {
			Factory.beginTransaction();

			Criteria criteria = Factory.createCriteria(UscisCaseStatus.class);
			criteria.add(Restrictions.ilike("uscisCaseStatusName", "%" + uscisCaseStatusName + "%"));
			criteria.setMaxResults(1);

			return (UscisCaseStatus) criteria.uniqueResult();
		} catch (Exception ex) {
			logger.error("Search UscisForm data error", ex);
		} finally {
			Factory.commitTransaction();
		}
		return null;
	}

	/**
	 * Save UscisCase
	 * 
	 * @param uscisCase
	 */
	public static void save(UscisCase uscisCase) {
		try {
			Factory.beginTransaction();
			Factory.saveOrUpdate(uscisCase);
			Factory.commitTransaction();
		} catch (Exception ex) {
			logger.error("Save UscisCase OBJ ERROR", ex);
			Factory.rollbackTransaction();
		}
	}

}